package com.sgs.soda.auth.controller;

import com.sgs.soda.auth.model.LoginReqDTO;
import com.sgs.soda.auth.model.LoginRespDTO;
import com.sgs.soda.core.GeneralResult;
import com.sgs.soda.core.auth.JwtTokenService;
import com.sgs.soda.core.auth.JwtUserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Tim_Tian
 * @date 2021/7/22 16:53
 */
@RestController
@RequestMapping("auth/login")
public class LoginController {

    @Autowired
    private JwtTokenService jwtTokenService;

    @RequestMapping(value = "doLogin", method = RequestMethod.POST)
    public GeneralResult<LoginRespDTO> doLogin(@RequestBody LoginReqDTO loginReq) {
        GeneralResult<LoginRespDTO> result = GeneralResult.<LoginRespDTO>newInstance().success();
        JwtUserDetail jwtUserDetail = new JwtUserDetail(123);

        long expireIn = 3600 * 24 * 365 * 1000;
        String token = jwtTokenService.generateToken(jwtUserDetail, expireIn);
        LoginRespDTO loginRespDTO = new LoginRespDTO();
        loginRespDTO.setAccessToken(token);
        loginRespDTO.setTokenType(jwtTokenService.getTokenType());
        loginRespDTO.setUserId(123L);
        result.setData(loginRespDTO);
        return result;
    }
}
